//
//  myglView.m
//  myfistopengl
//
//  Created by huiti123 on 2017/3/30.
//  Copyright © 2017年 wtw. All rights reserved.
//

#import "myglView.h"
#import "glutils.h"
#import <UIKit/UIKit.h>
#import "mygltexture.h"
#import "myglLineView.h"



@interface myglView (){

}
@end


@implementation myglView{
    GLuint  _program;
    
    CADisplayLink *_displayLink;
    
    
    GLElementFun  *glelement;
   
}


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        
       // glelement = [[myglLineView alloc]init];
        glelement = [[mygltexture alloc]init];
        
        [self setupProgram];
        [self setupGlParamenter];

    }
    
    return self;
    
}

-(void) layoutSubviews{
    [super layoutSubviews];
    glUseProgram(_program);
    [self render];
}

-(void) setupGlParamenter{

    [glelement setupGlParamenter:_program];
    
}


-(void) setupProgram{
    
 
    _program = [glelement getProgram];
    glUseProgram(_program);
}

-(void) drawElement{

    
    [glelement drawElements];
  //  glDrawArrays(GL_POINTS, 0, 4);
    
    //glDrawArrays(GL_TRIANGLE_STRIP, 1, 4);
    
}

-(void) render{
    [super render];
    
    glClear(GL_COLOR_BUFFER_BIT);
    glViewport(0, 0, self.frame.size.width, self.frame.size.height);
    
    
    [self drawElement];
    [_context presentRenderbuffer:GL_RENDERBUFFER ];
}


- (void)toggleDisplayLink
{
    if (_displayLink == nil) {
        _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkCallback:)];
        [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    }
    else {
        [_displayLink invalidate];
        [_displayLink removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        _displayLink = nil;
    }
}

- (void)displayLinkCallback:(CADisplayLink*)displayLink
{
    ///self.rotateX += displayLink.duration * 90;
}

@end
